package com.ml.mall.service.delivery.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ml.mall.bean.req.delivery.DeliveryEditReq;
import com.ml.mall.bean.req.delivery.DeliveryNewReq;
import com.ml.mall.entity.supplier.SysOrderSupplier;
import com.ml.mall.service.supplier.SysOrderSupplierService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ml.mall.mapper.delivery.SysOrderDeliveryMapper;
import com.ml.mall.entity.delivery.SysOrderDelivery;
import com.ml.mall.service.delivery.SysOrderDeliveryService;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class SysOrderDeliveryServiceImpl extends ServiceImpl<SysOrderDeliveryMapper, SysOrderDelivery> implements SysOrderDeliveryService{

    @Autowired
    private SysOrderDeliveryMapper sysOrderDeliveryMapper;

    @Autowired
    @Lazy
    private SysOrderSupplierService sysOrderSupplierService;

    @Override
    public SysOrderDelivery selectSysOrderDeliveryByOid(String supplierOid) {
        QueryWrapper<SysOrderDelivery> w1 = new QueryWrapper<>();
        w1.eq("supplier_oid",supplierOid);
        w1.eq("state",3);  //发货时间
        SysOrderDelivery sysOrderDelivery = sysOrderDeliveryMapper.selectOne(w1);
        return sysOrderDelivery;
    }

    @Override
    @Transactional
    public int sysOrderDeliveryNew(DeliveryNewReq req) {
        SysOrderDelivery entity = new SysOrderDelivery();
        entity.setSupplierOid(req.getSupplierOid());
        entity.setDeliveryName(req.getDeliveryName());
        entity.setDeliveryOid(req.getDeliveryOid());
        //entity.setOid(req.getOid());
        SysOrderSupplier sysOrderSupplier = sysOrderSupplierService.selectSysOrderSupplierBySupOid(req.getSupplierOid());
        if(sysOrderSupplier!=null){
            entity.setOid(sysOrderSupplier.getOid());
        }
        int insert = sysOrderDeliveryMapper.insert(entity);
        return insert;
    }

    @Override
    public int sysOrderDeliveryEdit(DeliveryEditReq req) {
        SysOrderDelivery sysOrderDelivery = new SysOrderDelivery();
        BeanUtils.copyProperties(req, sysOrderDelivery);
        return sysOrderDeliveryMapper.updateById(sysOrderDelivery);
    }
}
